package cn.ynrcb.personnelfile.sysadmin.controller;

import cn.ynrcb.personnelfile.common.annotation.SysLog;
import cn.ynrcb.personnelfile.common.utils.PageUtils;
import cn.ynrcb.personnelfile.common.utils.R;
import cn.ynrcb.personnelfile.sysadmin.dao.SysOrgDao;
import cn.ynrcb.personnelfile.sysadmin.entity.SysOrgEntity;
import cn.ynrcb.personnelfile.sysadmin.service.SysOrgService;
import cn.ynrcb.personnelfile.sysadmin.service.SysUserService;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 组织管理
 * @author shiwenbo
 * @email 282398188@qq.com
 * @date 2019-06-19 16:31:01
 */
@RestController
@RequestMapping("sys/org")
public class SysOrgController extends AbstractController{
    @Autowired
    private SysOrgService sysOrgService;
    @Autowired
    private SysOrgDao sysOrgDao;
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value="获取机构树形菜单", notes="获取所有机构的树形菜单")
    @GetMapping("/tree")
    public R select(){
        List<SysOrgEntity> list = sysOrgService.queryOrgTree();
        return R.ok().put("orgList", list);
    }

    @ApiOperation(value="根据机构ID获取下一级机构数据", notes="树形菜单异步加载，或者获取机构数据")
    @ApiImplicitParam(name="pid", value="机构ID", required = true, dataType = "Integer")
    @GetMapping("/treeData/{pid}")
    public R treeData(@PathVariable("pid") Integer pid){
        JSONArray data = sysOrgService.treeData(pid);
        return R.ok().put("data", data);
    }

    /**
     * 机构列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:org:list")
    public R list(@RequestParam Map<String, Object> params){
    	PageUtils page = sysOrgService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 机构下拉框选择列表
     */
    @GetMapping("/select")
    public R selectList(){
        List<SysOrgEntity> list = sysOrgService.list();
        return R.ok().put("list", list);
    }

    /**
     * 新增机构
     */
    @SysLog("新增机构")
    @PostMapping("/save")
    @RequiresPermissions("sys:org:save")
    public R save(@RequestBody SysOrgEntity org){
    	if(org.getParentId() > 0) {
    		org.setPId(org.getParentId());
    	}
    	org.setCreateUserId(getCurrentUserId());
		sysOrgService.saveOrg(org);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("sys:org:delete")
    public R delete(@RequestBody Integer[] ids){
		sysOrgService.updateStatusByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 获取用户组织树
     * @return
     */
    @GetMapping("/orgAndUserTree")
    @RequiresPermissions("sys:org:orgAndUserTree")
    public R orgAndUserTree(){
        List<SysOrgEntity> list = sysOrgService.orgAndUserTree();
        return R.ok().put("orgList", list);
    }

    /**
     * 机构详情
     */
    @ApiOperation("机构详情")
    @ApiImplicitParam(name="orgId", value="组织ID", required = true, example="20", dataType="Integer")
    @GetMapping("/info/{orgId}")
    @RequiresPermissions("sys:org:info")
    public R info(@PathVariable("orgId") Integer id){
        SysOrgEntity sysOrg = sysOrgService.getById(id);
        return R.ok().put("sysOrg", sysOrg);
    }

    /**
     * 修改机构信息
     */
    @SysLog("修改机构信息")
    @ApiOperation(value="修改机构", notes="根据实体对象更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "SysOrgEntity", value = "机构详细实体SysOrgEntity", required = true, dataType = "SysOrgEntity")
    })
    @PutMapping("/update")
    @RequiresPermissions("sys:org:update")
    public R update(@RequestBody SysOrgEntity sysOrg){
        sysOrg.setUpdateUserId(getCurrentUserId());
        sysOrg.setUpdateTime(new Date());
        sysOrg.setCreateUserId(sysOrgService.getById(sysOrg.getId()).getCreateUserId());
        sysOrgService.updateById(sysOrg);
        return R.ok();

    }
}
